package com.oyjd.framework.ui.menu;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.oyjd.R;

/**
 * 左右滑动菜单
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class MenuBar extends LinearLayout {
	private static int dix = 10;// 左右边界灵敏差值
	private LayoutInflater flater;// 布局加载器
	private HorizontalScrollView scrollBar;
	private View arrow_left;// 左箭头
	private View arrow_right;// 右箭头
	private LinearLayout linear;// 菜单容器
	private OnMenuClickListener listener;// 菜单监听器
	private View currentMenu = null;// 当前选中菜单

	/**
	 * 构造函数
	 * 
	 * @param context
	 */
	public MenuBar(Context context) {
		super(context);
		init();
	}

	/**
	 * 构造函数
	 * 
	 * @param context
	 * @param attrs
	 */
	public MenuBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	/**
	 * 初始化
	 */
	private void init() {
		// 初始化布局加载器
		flater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		flater.inflate(R.layout.inner_menu_bar, this, true);

		scrollBar = (HorizontalScrollView) this.findViewById(R.id.scroll_bar);
		arrow_left = this.findViewById(R.id.arrow_left);
		arrow_right = this.findViewById(R.id.arrow_right);
		linear = (LinearLayout) scrollBar.getChildAt(0);
		scrollBar.fling(1000);

		// 定时判断目标的scrollX来控制左右两边的图片显隐,注意用setOnTouchListener不好
		t.start();
	}

	/**
	 * 设置菜单项
	 * 
	 * @param data
	 */
	public void setMenuData(List<MenuVO> data, int startPos) {
		if (startPos < 0 || startPos > data.size()) {
			startPos = 0;
		}
		linear.removeAllViews();
		for (MenuVO vo : data) {
			linear.addView(createMenu(vo));
		}

		setCurrentMenu(linear.getChildAt(startPos));
	}

	/**
	 * 刷新箭头
	 */
	private void refreshArrow() {
		if (scrollBar.getScrollX() < dix) {
			arrow_left.setVisibility(View.GONE);
		} else {
			arrow_left.setVisibility(View.VISIBLE);
		}
		if (scrollBar.getScrollX() + scrollBar.getWidth() > linear.getWidth() - dix) {
			arrow_right.setVisibility(View.GONE);
		} else {
			arrow_right.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 内部菜单点击监听器
	 */
	private View.OnClickListener onClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			setCurrentMenu(v);
			if (null != listener) {
				listener.menuClick((MenuVO) v.getTag());
			}

		}
	};

	/**
	 * 设置点击监听
	 * 
	 * @param listener
	 */
	public void setOnMenuClickListener(OnMenuClickListener listener) {
		this.listener = listener;
	}

	/**
	 * 点击菜单监听器
	 * 
	 * @author 欧建东
	 * @email oyjd@163.com
	 * 
	 */
	public interface OnMenuClickListener {
		void menuClick(MenuVO menuVO);
	}

	/**
	 * 创建单个菜单视图
	 * 
	 * @param vo
	 * @return
	 */
	private View createMenu(MenuVO vo) {
		Button bt = new Button(getContext());
		bt.setText(vo.txt);
		bt.setPadding(bt.getPaddingLeft(), 0, bt.getPaddingRight(), 0);
		bt.setGravity(Gravity.CENTER);
		bt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
		bt.setBackgroundColor(Color.TRANSPARENT);
		bt.setTextColor(Color.WHITE);
		bt.setOnClickListener(onClickListener);

		bt.setTag(vo);
		return bt;
	}

	/**
	 * 设置当前选中菜单
	 * 
	 * @param pos
	 */
	private void setCurrentMenu(View menu) {
		if (null != currentMenu && currentMenu.equals(menu)) {
			return;
		}

		if (null != currentMenu) {
			Button bt = (Button) currentMenu;
			bt.setBackgroundColor(Color.TRANSPARENT);
			bt.setTextColor(Color.WHITE);
		}

		Button bt = (Button) menu;
		bt.setBackgroundResource(R.drawable.scroll_menu_button);
		bt.setTextColor(Color.parseColor("#0099cc"));

		currentMenu = menu;
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			refreshArrow();
		}

	};

	private Thread t = new Thread(new Runnable() {
		@Override
		public void run() {
			while (true) {
				Message msg = new Message();
				handler.sendMessage(msg);
				try {
					Thread.sleep(500L);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

		}
	});

}
